package org.one.stone.soup.grfxML.applet;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.net.HttpURLConnection;

import javax.imageio.ImageIO;
import javax.swing.JApplet;

import org.one.stone.soup.browser.Browser;
import org.one.stone.soup.browser.BrowserFileOutputStream;
import org.one.stone.soup.grfx.ImageFactory;
import org.one.stone.soup.grfxML.APInterface;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.GrfxMLController;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.GrfxMLView;
import org.one.stone.soup.grfxML.ui.GrfxMLJComponent;
import org.one.stone.soup.xml.XmlDocument;

public class GiraffeApplet extends JApplet implements GrfxMLController{
	
	private GrfxMLJComponent giraffe;
	private GrfxMLEngine engine;
	private APInterface api;
	
	public GiraffeApplet()
	{		
		ImageFactory.root = GiraffeApplet.class;

		Browser.initialize(true,"grfxml");
		String alias = "Giraffe Applet";
		
		XmlDocument settings = new XmlDocument("settings");
		int width = this.getWidth();
		int height = this.getHeight();
		
		engine = new GrfxMLEngine(this,alias,settings);
		giraffe = new GrfxMLJComponent(engine,this,width,height);
		engine.setView(giraffe);
		
		getContentPane().add(giraffe,BorderLayout.CENTER);

		api = APInterface.getApi("Giraffe Applet");		
	}

	public void init()
	{
		this.getCodeBase();
		String url = getParameter("url");
		if(url==null)
		{
			url = "jar://resources/giraffe/modeler/modeler.grfxml";
		}
		else if(url.charAt(0)=='.')
		{
			url = getCurrentUrl()+url.substring(1);
		}
		System.out.println("Opening "+url);
		api.load(url);		
	}
	
	public void open(String url)
	{
		if(url.charAt(0)=='.')
		{
			url = getCurrentUrl()+url.substring(1);
		}

		api.load(url);
	}
	
	public boolean choose(String[] text) {
		// TODO Auto-generated method stub
		return false;
	}

	public Component getComponent() {
		return giraffe;
	}

	public Point getMouseOffset() {
		return new Point(0,0);
	}

	public String getValue(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public GrfxMLView getView() {
		return giraffe;
	}

	public void sendCommand(String command) {
	}

	public void setReady(GrfxMLEngine engine) {
		// TODO Auto-generated method stub
		
	}

	public void setTitle(String title) {
		// TODO Auto-generated method stub
		
	}

	public void setWaiting(boolean state) {
		// TODO Auto-generated method stub
		
	}

	public void showConsole(boolean state) {
		// TODO Auto-generated method stub
		
	}

	public void showLoadError(Exception e, Object resourceName) {
		// TODO Auto-generated method stub
		
	}

	public void showWebPage(String url) {
		// TODO Auto-generated method stub
		
	}

	public String userGetFile(String label, String[] masks) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getSource()
	{
		return api.getBase().togrfxML("grfxML");
	}
	
	public void savePng( String pageName,String fileName )
	{
		try{
			((DataState)api.getField("showGui")).setValue(false,this);
			
			BufferedImage image = (BufferedImage)giraffe.getPreImage();

			BrowserFileOutputStream outStream = new BrowserFileOutputStream(getRootUrl()+"/OpenForum/Actions/Attach?page="+pageName,fileName);
			
			ImageIO.write(image,"PNG",outStream);

			outStream.flush();
			outStream.close();
			
			outStream.getInputStream();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{
				((DataState)api.getField("showGui")).setValue(true,this);
			}
			catch(Exception e){}
		}
	}
	
	public String getCurrentUrl()
	{
		return this.getCodeBase().toExternalForm();
	}	
	
	public String getRootUrl()
	{
		return "http://"+this.getCodeBase().getHost();
	}
}
